//
//  happy_num.hpp
//  cpp_algorithm
//
//  Created by 李贺 on 2021/12/8.
//

#ifndef happy_num_hpp
#define happy_num_hpp

#include <stdio.h>
#include <vector>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

class Solution_happy_num {
    
    
public:
    
    int getSum(int n) {
        int sum = 0;
        while (n) {
            sum = (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }
    
    bool is_happy(int num) {
        
        unordered_set<int> numset;
        int value = num;
        while (true) {
            
            int sum = getSum(value);
            if (sum == 1) {
                return true;
            }
            
            if (numset.find(sum) != numset.end()) {
                return false;
            } else {
                numset.insert(sum);
            }
            value = sum;
        }
        
        return false;
    }
    
};

#endif /* happy_num_hpp */
